package com.triones.ljr.controller.web;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.triones.ljr.entity.bo.PolicyParams;
import com.triones.ljr.entity.dto.PolicyDO;
import com.triones.ljr.entity.dto.PolicyItemDO;
import com.triones.ljr.entity.vo.*;
import com.triones.ljr.service.PolicyService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 我要获策
 *
 * @Author: triones
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/policy")
public class PolicyController extends BaseController {

    private final PolicyService policyService;

    /**
     * 列表（分页）
     */
    @SaCheckPermission("system:policy:pageList")
    @GetMapping("/pageList")
    public TableDataInfo<CustomPolicyVO> pageList(PolicyParams params, PageQuery pageQuery) {
        return policyService.pageList(params, pageQuery);
    }

    /**
     * 新增
     */
    @SaCheckPermission("system:policy:add")
    @Log(title = "我要获策", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody PolicyDO dto) {
        return toAjax(policyService.add(dto));
    }

    /**
     * 获取详情
     */
    @GetMapping("/{id}")
    public R<PolicyVO> getInfo(@PathVariable Long id) {
        return R.ok(policyService.getInfo(id));
    }

    /**
     * 修改
     */
    @SaCheckPermission("system:policy:edit")
    @Log(title = "我要获策", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody PolicyDO dto) {
        return toAjax(policyService.edit(dto));
    }

    @GetMapping("/list")
    public R<List<PolicyVO>> list() {
        return R.ok(policyService.list());
    }

    /**
     * 新增-政策细则
     */
    @SaCheckPermission("system:policyItem:add")
    @Log(title = "我要获策-政策细则", businessType = BusinessType.INSERT)
    @PostMapping("/item")
    public R<Void> addItem(@Validated @RequestBody PolicyItemDO dto) {
        if (!policyService.checkItemNameUnique(dto)) {
            return R.fail("新增政策细则'" + dto.getItemName() + "'失败，政策细则名称已存在");
        }
        return toAjax(policyService.addItem(dto));
    }

    /**
     * 政策细则-列表（分页）
     */
    @SaCheckPermission("system:policyItem:pageList")
    @GetMapping("/item/pageList")
    public TableDataInfo<CustomPolicyItemVO> itemPageList(PolicyItemDO dto, PageQuery pageQuery) {
        return policyService.itemPageList(dto, pageQuery);
    }

    /**
     * 编辑-政策细则
     */
    @SaCheckPermission("system:policyItem:edit")
    @Log(title = "我要获策-政策细则", businessType = BusinessType.INSERT)
    @PutMapping("/item")
    public R<Void> editItem(@Validated @RequestBody PolicyItemDO dto) {
        if (!policyService.checkItemNameUnique(dto)) {
            return R.fail("修改政策细则'" + dto.getItemName() + "'失败，政策细则名称已存在");
        }
        return toAjax(policyService.editItem(dto));
    }

    /**
     * 政策细项-获取详情
     */
    @GetMapping("/item/{id}")
    public R<PolicyItemVO> getItemInfo(@PathVariable Long id) {
        return R.ok(policyService.getItemInfo(id));
    }

    /**
     * 政策细项-删除
     * @param id
     */
    @SaCheckPermission("system:policyItem:remove")
    @Log(title = "我要获策-政策细则", businessType = BusinessType.INSERT)
    @DeleteMapping("/item/{id}")
    public R<Void> removeItem(@PathVariable Long id) {
        return toAjax(policyService.removeItem(id));
    }

    /**
     * 删除
     * @param id
     */
    @SaCheckPermission("system:policy:remove")
    @Log(title = "我要获策", businessType = BusinessType.INSERT)
    @DeleteMapping("/{id}")
    public R<Void> remove(@PathVariable Long id) {
        return toAjax(policyService.remove(id));
    }
}
